package com.example.contactprovidertest;

import android.Manifest;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.RawContacts.Data;


import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button search = (Button) findViewById(R.id.search);
        Button add = (Button) findViewById(R.id.add);
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                        0x123);
            }
        });
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS},
                        0x456);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull  String[] permissions,
                                           @NonNull int[] grantResults){
        if (requestCode == 0x123){
            search();
        }else if (requestCode == 0x456){
            add();
        }else {
            Toast.makeText(MainActivity.this,R.string.permission_tip,Toast.LENGTH_SHORT).show();
        }
    }
    protected void search() {
        // 定义两个List来封装系统的联系人信息、指定联系人的电话号码、Email等详情
        final ArrayList<String> names = new ArrayList<>();
        final ArrayList<ArrayList<String>> details = new ArrayList<>();
        // 使用ContentResolver查找联系人数据
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
                null, null, null, null);
        // 遍历查询结果，获取系统中所有联系人
        while (cursor.moveToNext()) {
            // 获取联系人ID
            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            // 获取联系人的名字
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            names.add(name);
            // 使用ContentResolver查找联系人的电话号码
            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
                    null, null);
            ArrayList<String> detail = new ArrayList<>();
            // 遍历查询结果，获取该联系人的多个电话号码
            while (phones.moveToNext()) {
                // 获取查询结果中点好号码列中数据
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                detail.add("电话号码：" + phoneNumber);
            }
            phones.close();
            // 使用ContentResolver查找联系人的E-mail地址
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
                    null, null);
            // 遍历查询结果，获取该联系人的多个E-Mail地址
            while (emails.moveToNext()) {
                // 获取查询结果中Email地址列表中数据
                String emailAdress = emails.getString(emails.
                        getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                detail.add("邮件地址：" + emailAdress);
            }
            emails.close();
            details.add(detail);
        }
        cursor.close();
        // 加载result.xml界面布局代表的视图
        View resultDiaglog = getLayoutInflater().inflate(R.layout.result, null);
        // 获取resultDialog中ID为list的ExpandableListView
        ExpandableListView list = (ExpandableListView) resultDiaglog.findViewById(R.id.list);
        // 创建一个ExpandableListAdapter对象
        ExpandableListAdapter adapter = new BaseExpandableListAdapter() {

            // 获取指定组位置、指定子列表项的数据
            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return details.get(groupPosition).get(childPosition);
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return details.get(groupPosition).size();
            }

            private TextView getTextView() {
                AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, 64);
                TextView textView = new TextView(MainActivity.this);
                textView.setLayoutParams(lp);
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                textView.setPadding(36, 0, 0, 0);
                textView.setTextSize(20);
                return textView;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition,
                                     boolean isLastChild, View convertView, ViewGroup parent) {
                TextView textView = getTextView();
                textView.setText(getChild(groupPosition, childPosition).toString());
                return textView;
            }

            // 获取指定组位置处的组数据
            @Override
            public Object getGroup(int groupPosition) {
                return names.get(groupPosition);
            }

            @Override
            public int getGroupCount() {
                return names.size();
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            // 该方法决定每个组选项的外观
            @Override
            public View getGroupView(int groupPosition,
                                     boolean isExpanded, View convertView,
                                     ViewGroup parent) {
                TextView textView = getTextView();
                textView.setText(getGroup(groupPosition).toString());
                return textView;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return true;
            }
        };
        // 为ExpandableListView设置Adapter对象
        list.setAdapter(adapter);
        new AlertDialog.Builder(MainActivity.this).
                setView(resultDiaglog).
                setPositiveButton("确定", null).show();
    }

    protected void add() {
        // 获取程序界面的三个文本框的内容
        String name = ((EditText)findViewById(R.id.name)).getText().toString();
        String phone = ((EditText)findViewById(R.id.phone)).getText().toString();
        String email = ((EditText)findViewById(R.id.email)).getText().toString();
        // 创建一个空的ContentValues
        ContentValues values = new ContentValues();
        // 向RawContacts.CONTENT_URI 执行一个空值插入
        // 目的是获取系统返回的rawContactId
        Uri rawContactUri = getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI,values);
        long rawContactId = ContentUris.parseId(rawContactUri);
        values.clear();
        values.put(Data.RAW_CONTACT_ID,rawContactId);
        // 设置内容类型
        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        // 设置联系人名字
        values.put(StructuredName.GIVEN_NAME,name);
        // 向联系人URI添加联系人名字
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI,values);
        values.clear();
        values.put(Data.RAW_CONTACT_ID,rawContactId);
        values.put(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE);
        // 设置联系人电话号码
        values.put(Phone.TYPE,phone);
        // 设置电话类型
        values.put(Phone.TYPE,Phone.TYPE_MOBILE);
        // 向联系人电话号码URi添加电话号码
        getContentResolver().insert(ContactsContract.Data.CONTENT_URI,values);
        values.clear();
        values.put(Data.RAW_CONTACT_ID, rawContactId);
        values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
        // 设置联系人的E-mail地址
        values.put(Email.DATA, email);
        // 设置该电子邮件的类型
        values.put(Email.TYPE, Email.TYPE_WORK);
        // 向联系人E-mail URI添加E-mail数据
        getContentResolver().insert(android.provider.ContactsContract
                .Data.CONTENT_URI, values);
        Toast.makeText(MainActivity.this, "联系人数据添加成功",
                Toast.LENGTH_SHORT).show();
    }
}
